package org.jeecg.modules.htxincailiao.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.htxincailiao.address.domain.vo.HTAddressVO;
import org.jeecg.modules.htxincailiao.address.service.IHTShopAddressService;
import org.jeecg.modules.htxincailiao.industry.entity.HTIndustryCategory;
import org.jeecg.modules.htxincailiao.industry.entity.HTIndustrySalon;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustryCategoryService;
import org.jeecg.modules.htxincailiao.shop.entity.HTShop;
import org.jeecg.modules.htxincailiao.shop.mapper.HTShopMapper;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.util.CollectionUtils;

/**
 * @Description: ht_shop
 * @Author: jeecg-boot
 * @Date:   2021-12-10 03:26:26
 * @Version: V1.0
 */
@Service
public class HTShopServiceImpl extends ServiceImpl<HTShopMapper, HTShop> implements IHTShopService {

    @Resource
    private IHTShopAddressService shopAddressService;

    @Resource
    private IHTIndustryCategoryService industryCategoryService;

    @Override
    public HTShop getShopInfo(String id) {
        LambdaQueryWrapper<HTShop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HTShop::getId, id);
        queryWrapper.or();
        queryWrapper.eq(HTShop::getSellerId, id);
        HTShop hTShop = null;
        List<HTShop> hTShopList = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(hTShopList)) {
            hTShop = hTShopList.get(0);
        }

        if (oConvertUtils.isNotEmpty(hTShop) ){
            String industryCategoryId = hTShop.getIndustryCategoryId();
            if( oConvertUtils.isNotEmpty(industryCategoryId)) {
                HTIndustryCategory category = industryCategoryService.getById(industryCategoryId);
                if (oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName())) {
                    String industryCategoryName = industryCategoryService.getById(industryCategoryId).getName();
                    hTShop.setIndustryCategoryName(industryCategoryName);
                }
            }

            if(oConvertUtils.isNotEmpty(hTShop.getShopAddressId())){
                HTAddressVO address = shopAddressService.queryByAddressId(hTShop.getShopAddressId());
              if (oConvertUtils.isNotEmpty(address)) {
                  hTShop.setAddress(address);
              }
            }
        }


        return hTShop;
    }

    @Override
    public HTShop queryByShopName(String shopName) {
        return baseMapper.queryByShopName(shopName);
    }

    @Override
    public HTShop queryBySellerId(String sellerId) {
        return baseMapper.queryBySellerId(sellerId);
    }
}
